//
//  LBNewCategorySearchView.m
//  AFNetworking
//
//  Created by luckyRoy on 2018/2/27.
//

#import "LBNewCategorySearchView.h"
#import "Masonry.h"
#import "UIColor+LBColor.h"

@interface LBNewCategorySearchView ()<UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) UIButton *clickButton;

@property (nonatomic, copy) LBNewCategorySearchViewBlock complete;

@end

@implementation LBNewCategorySearchView

- (instancetype)initWithFrame:(CGRect)frame
                     complete:(LBNewCategorySearchViewBlock)complete
{
    self = [super initWithFrame:frame];
    if (self) {
        self.complete = complete;
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
//    CGFloat space = 10;
//    self.backgroundColor = [UIColor colorWithRed:243/255.0 green:246/255.0 blue:249/255.0 alpha:1];

    [self addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.mas_equalTo([self.class heightForView]);
//        make.centerY.equalTo(self);
//        make.bottom.offset(-space);
    }];
    
    self.backgroundColor = [UIColor red];
    
    [self.searchBar addSubview:self.clickButton];
    [self.clickButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.searchBar);
    }];
}

#pragma mark - click

- (void)buttonClick:(UIButton *)senter
{
    if (self.complete) {
        self.complete(nil);
    }
}

#pragma mark - setter

#pragma mark - getter

- (UIImage *)imageWithColor:(UIColor*)color size:(CGSize)size{
    
           CGRect rect =CGRectMake(0,0, size.width, size.height);
    
           UIGraphicsBeginImageContext(rect.size);
    
           CGContextRef context =UIGraphicsGetCurrentContext();
    
           CGContextSetFillColorWithColor(context,color.CGColor);
    
           CGContextFillRect(context, rect);
    
           UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    
           UIGraphicsEndImageContext();
    
           return image;

}

- (UISearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.tintColor = [UIColor red];
        _searchBar.showsCancelButton = NO;
        _searchBar.placeholder = @"搜索分类名称";
        _searchBar.searchBarStyle = UISearchBarStyleDefault;
        UIColor *backColor = [UIColor colorWithRed:243/255.0 green:246/255.0 blue:249/255.0 alpha:1];
        _searchBar.backgroundImage = [self imageWithColor:backColor size:CGSizeMake(1,1)];
    }
    
    return _searchBar;
}

- (UIButton *)clickButton
{
    if (!_clickButton) {
        _clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _clickButton.backgroundColor = [UIColor clearColor];
        [_clickButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clickButton;
}

+ (CGFloat)heightForView
{
    return 60;
}

@end
